//
//  ChatToolBar.m
//  ChatTool
//
//  Created by zhuxuehang on 13-10-9.
//  Copyright (c) 2013年 zhuxuehang. All rights reserved.
//

#import "ChatToolBar.h"
//#import "EmotionView.h"
#import "ChatToolMoreView.h"
#import "MicroDef.h"
#import "UIUtil.h"
#import "CHSendNotification.h"
#import "MBProgressHUD.h"
#import "CHChatRoomBase.h"
#define ChatToolPosY  114.0f
#define EmotionPosY    64.0f
#define EmotionHeight  180.0f
#define MoreHeight 200
#define EdgeSpaceLeft   (HARDWARE_SCREEN_WIDTH-300)/2

#define MaxLines 13
#define MaxHeight 16+MaxLines*22

static ChatToolBar* shared;

@implementation ChatToolBar
@synthesize moreBtn = _moreBtn;
@synthesize voiceChangeBtn = _voiceChangeBtn;
@synthesize voiceBtn = _voiceBtn;
@synthesize sendBtn = _sendBtn;
@synthesize emotionBtn = _emotionBtn;
@synthesize contentText = _contentText;
@synthesize chattoolDelegate = _chattoolDelegate;
@synthesize isEmotionShow,isMoreViewShow,isVoiceShow;
@synthesize imageView = _imageView;
@synthesize proToolHeight = _proToolHeight;


+(ChatToolBar*)defaultChatToolBar
{
    @synchronized(self)
    {
        if(shared == nil)
        {
            shared = [[ChatToolBar alloc] init];
        }
    }
    return shared;
}


- (id)init
{
    self = [super init];
    if (self) {
        [CNMultiThemeManager registerForThemeChange:self];
        self.backgroundColor = RGBCOLOR(226, 226, 226);
        self.frame = CGRectMake(0,HARDWARE_SCREEN_HEIGHT - 44 - [UIApplication sharedApplication].statusBarFrame.size.height - 50, HARDWARE_SCREEN_WIDTH, 50);
        self.animateCurve = UIViewAnimationCurveEaseInOut;
        self.animateDuration = 0.25f;
        isVoiceShow = YES;
        isEmotionShow = YES;
        [self setDesign];
        [self loadNotifications];
    }
    return self;
}

-(void)setDesign
{
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, 0.5f)];
    line.backgroundColor = RGBCOLOR(212, 212, 212);
    [self addSubview:line];
    
    _voiceChangeBtn = [self buttonWithFrame:CGRectMake(0, 0, 44, 50) normalImg:@"chatviewtool_voice.png" selectedImg:@"chatviewtool_voice" tag:104 action:@selector(voiceChangeMethod:)];
    
    [self addSubview:_voiceChangeBtn];
    _contentText = [[UIExpandingTextView alloc] initWithFrame:CGRectMake(_voiceChangeBtn.frame.origin.x+_voiceChangeBtn.frame.size.width,6 , HARDWARE_SCREEN_WIDTH - (_voiceChangeBtn.frame.origin.x+_voiceChangeBtn.frame.size.width) - 78, 44)];
    [_contentText setDelegate:self];
    [_contentText setReturnKeyType:UIReturnKeySend];
    [_contentText setMaximumNumberOfLines:4];
//    [_contentText setAnimateHeightChange:YES];
    [self addSubview:_contentText];

    CGRect bgfra = _contentText.frame;
    bgfra.size.height -= 6;

    
    _voiceBtn = [self buttonWithFrame:bgfra normalImg:@""  selectedImg:@"" tag:102 action:nil];
    //[_voiceBtn setImage:[UIImage imageNamed:@"chatviewtool_h.png"] forState:UIControlStateHighlighted];
    [_voiceBtn setBackgroundImageStr:@"speakButton" forState:UIControlStateNormal];
    [_voiceBtn setTitle:@"按住 语音通话" forState:UIControlStateNormal];
    [_voiceBtn setTitle:@"松开 发送语音" forState:UIControlStateHighlighted];
    [_voiceBtn setTitleColor:BlackColor forState:UIControlStateNormal];
    [_voiceBtn setTitleColor:BlackColor forState:UIControlStateHighlighted];
    _voiceBtn.titleLabel.font = [UIFont systemFontOfSize:14.0f];

    
    
    [_voiceBtn addTarget:self action:@selector(btnDown:) forControlEvents:UIControlEventTouchDown];
    [_voiceBtn addTarget:self action:@selector(btnUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [_voiceBtn addTarget:self action:@selector(btnDragExit:) forControlEvents:UIControlEventTouchDragExit];
    [_voiceBtn addTarget:self action:@selector(btnDragEnter:) forControlEvents:UIControlEventTouchDragEnter];
    [_voiceBtn addTarget:self action:@selector(btnUpOutside:) forControlEvents:UIControlEventTouchUpOutside];
    [_voiceBtn setHidden:YES];
    [self insertSubview:_voiceBtn belowSubview:_contentText];


    
    _emotionBtn = [self buttonWithFrame:CGRectMake(_contentText.frame.origin.x+_contentText.frame.size.width, 0, 39, 50) normalImg:@"chatviewtool_expression.png" selectedImg:@"chatviewtool_expression.png" tag:101 action:@selector(emotionBtnMethod:)];
    [self addSubview:_emotionBtn];
    
    
    _moreBtn = [self buttonWithFrame:CGRectMake(CGRectGetMaxX(_emotionBtn.frame), 0, 39, 50) normalImg:@"chatviewtool_more.png" selectedImg:@"chatviewtool_more.png" tag:100 action:@selector(moreBtnMethod:)];
    [self addSubview:_moreBtn];

    
    
       [EmotionView defaultEmoView].delegate = self;
}

- (ISTThemeButton *)buttonWithFrame:(CGRect)frame normalImg:(NSString*)normalImg selectedImg:(NSString*)selectedImg tag:(int)tagValue action:(SEL)sel
{
    ISTThemeButton *btn = [[ISTThemeButton alloc] initWithFrame:CGRectZero];
    [btn setFrame:frame];
    [btn setImageStr:normalImg forState:UIControlStateNormal];
    [btn setImageStr:selectedImg forState:UIControlStateSelected];
    [btn setImageStr:selectedImg forState:UIControlStateHighlighted];
    
    [btn setTag:tagValue];
    [btn addTarget:nil action:sel forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

- (ISTThemeButton *)buttonWithFrame:(CGRect)frame normalImg:(NSString*)normalImg selectedImg:(NSString*)selectedImg text:(NSString*)text tag:(int)tagValue action:(SEL)sel
{
    ISTThemeButton *btn = [[ISTThemeButton alloc] initWithFrame:CGRectZero];
    [btn setFrame:frame];
    [btn setBackgroundImageStr:normalImg forState:UIControlStateNormal];
    [btn setTitle:text forState:UIControlStateNormal];
    // [btn setBackgroundImage:selectedImg forState:UIControlStateSelected];
    [btn setTag:tagValue];
    [btn addTarget:nil action:sel forControlEvents:UIControlEventTouchUpInside];
    return btn;
}


- (void)emotionViewDidSend
{
    [self sendBtnMethod:nil];
}

- (void)setChatText:(NSString *)ChatText
{
    _ChatText = ChatText;
    _contentText.text = ChatText;
    
}

#pragma mark- uiexpandtextviewdelegate

-(BOOL)expandingTextView:(UIExpandingTextView *)expandingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    [expandingTextView.internalTextView setSelectedRange:range];
    return YES;
}
-(BOOL)expandingTextViewShouldBeginEditing:(UIExpandingTextView *)expandingTextView
{
    return YES;
}
- (BOOL)expandingTextViewShouldReturn:(UIExpandingTextView *)expandingTextView
{
    [self sendBtnMethod:nil];
    return YES;
}
- (void)expandingTextViewDidBeginEditing:(UIExpandingTextView *)expandingTextView{
    [_voiceChangeBtn setHidden:NO];
    
    if (isMoreViewShow) {
        ChatToolMoreView* moreView = [ChatToolMoreView defaultMoreView];
        CGRect frame = moreView.frame;
        frame.origin.y = HARDWARE_SCREEN_HEIGHT-EmotionPosY;
        [moreView setFrame:frame];
        isMoreViewShow = !isMoreViewShow;
    }
    if (!isEmotionShow) {
        self.textSelectRange = NSMakeRange(0, 0);
        
        EmotionView* emo = [EmotionView defaultEmoView];
        CGRect emoFrame = emo.frame;
        emoFrame.origin.y = HARDWARE_SCREEN_HEIGHT-EmotionPosY;

        [emo setFrame:emoFrame];
        isEmotionShow = !isEmotionShow;
    }
    if (!isVoiceShow) {
        [_voiceChangeBtn setImageStr:@"chatviewtool_voice.png" forState:UIControlStateNormal];
        [_voiceChangeBtn setImageStr:@"chatviewtool_voice.png" forState:UIControlStateSelected];
        [_contentText setHidden:NO];
        isVoiceShow = !isVoiceShow;
    }

    
}

-(void)expandingTextViewDidEndEditing:(UIExpandingTextView *)expandingTextView{
    
    
}

- (void)expandingTextViewDidChange:(UIExpandingTextView *)expandingTextView
{
//    NSLog(@"%@",expandingTextView.text);
    
    
    if (![self checkTextCount:expandingTextView.text] &&
        ([self.chattoolDelegate isKindOfClass:[CHSendNotification class]] || [self.chattoolDelegate isKindOfClass:[CHChatRoomBase class]]))
    {
        NSInteger index = 0;
        if([UIApplication sharedApplication].windows.count > 1)index=1;
        UIWindow *win=[[UIApplication sharedApplication].windows objectAtIndex:index];
        MBProgressHUD *progress = [[MBProgressHUD alloc] initWithView:win];
        progress.removeFromSuperViewOnHide = YES;
        progress.labelText = [NSString stringWithFormat:@"发送的文本不能超过%d字",kMaxTextLength];
        [progress setAlpha:0.8];
        progress.mode = MBProgressHUDModeText;
        [win addSubview:progress];
        [win bringSubviewToFront:progress];
        [progress show:YES];
        [progress hide:YES afterDelay:1];
        
        expandingTextView.text = [expandingTextView.text substringToIndex:kMaxTextLength];

    }
    [expandingTextView scrollRangeToVisible:expandingTextView.selectedRange];
    
    if ([self.chattoolDelegate respondsToSelector:@selector(contentTextViewTextDidChangeWithText:)]) {
        [self.chattoolDelegate contentTextViewTextDidChangeWithText:expandingTextView.text];
    }
}

- (BOOL)checkTextCount:(NSString *)text
{
    NSInteger length = text.length;
    if (length>kMaxTextLength) {
        return NO;
    }
    return YES;
}




- (void)expandingTextView:(UIExpandingTextView *)expandingTextView willChangeHeight:(float)height
{
    
//    CGRect fra = _contentText.frame;
//    fra.size.width =  HARDWARE_SCREEN_WIDTH - (_voiceChangeBtn.frame.origin.x+_voiceChangeBtn.frame.size.width) - 78;
//    [_contentText setFrame:fra];
//    CGFloat hei = expandingTextView.frame.size.height;
//    if (!isEmotionShow) {
//        if (height>38&&height<=MaxHeight) {
//            CGRect frame = self.frame;
//            frame.origin.y = HARDWARE_SCREEN_HEIGHT-70-hei-EmotionHeight;
//            frame.size.height = hei+6;
//            [self setFrame:frame];
//            frame.origin.x = 0;
//            frame.origin.y = 0;
//            [_imageView setFrame:frame];
//            
//            _contentText.internalTextView.contentOffset=CGPointMake(0,_contentText.internalTextView.contentSize.height-_contentText.internalTextView.frame.size.height);
//        }
//    }
}
- (void)expandingTextView:(UIExpandingTextView *)expandingTextView didChangeHeight:(float)height
{
    if (!_contentText.internalTextView.isFirstResponder && expandingTextView.text.length <= 0 && isEmotionShow) {
        return;
    }
    CGFloat hei = expandingTextView.frame.size.height;
    
    if ( hei>44 && hei <= MaxHeight) {
        CGRect frame = self.frame;
        if (!isEmotionShow) {
            frame.origin.y = HARDWARE_SCREEN_HEIGHT-70-hei-EmotionHeight;
            frame.size.height = hei+6;
        }else
        {
            frame.origin.y = HARDWARE_SCREEN_HEIGHT-70-hei-_KeyBoardHeight;
            frame.size.height = hei+6;
        }
        [self setFrame:frame];
        
    } else if (hei<=44) {
        
        CGRect frame = self.frame;
        if (!isEmotionShow) {
            frame.origin.y = HARDWARE_SCREEN_HEIGHT-70-hei-EmotionHeight;
        }else {
            frame.origin.y = HARDWARE_SCREEN_HEIGHT-70-hei-_KeyBoardHeight;
        }
        frame.size.height = hei+6;
        [self setFrame:frame];
    }
    
    
    [[NSNotificationCenter defaultCenter] postNotificationName:ChatToolBarMoved object:[NSString stringWithFormat:@"%f",hei - 44]];
    
}


#pragma mark- --方法--
- (void)moreBtnMethod:(id)sender
{
    if (!isVoiceShow) {
        [_voiceChangeBtn setImageStr:@"chatviewtool_voice.png" forState:UIControlStateNormal];
        
        [_voiceChangeBtn setImageStr:@"chatviewtool_voice.png" forState:UIControlStateSelected];
        
//        [_voiceChangeBtn setImageStr:@"chatviewtool_voice-press.png" forState:UIControlStateHighlighted];
        
        
        [_voiceBtn setHidden:YES];
        [_contentText setHidden:NO];
        //        [_contentTextBg setHidden:NO];
        isVoiceShow = !isVoiceShow;
    }
    if (!isEmotionShow) {
        EmotionView* moreView = [EmotionView defaultEmoView];
        
        CGRect frame = moreView.frame;
        frame.origin.y = HARDWARE_SCREEN_HEIGHT-EmotionPosY;
        [moreView setFrame:frame];
   
        isEmotionShow = !isEmotionShow;
//            [moreView setHidden:YES];
    }
    
    if (_contentText.internalTextView.isFirstResponder) {
        [_contentText.internalTextView resignFirstResponder];
    }

     [self moreViewShow];
}

- (void)moreViewShow
{
    ChatToolMoreView* emo = [ChatToolMoreView defaultMoreView];
    if (isMoreViewShow) {       //消失
        //        [_sendBtn setHidden:YES];
        [_voiceChangeBtn setHidden:NO];
        
        
        
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:self.animateDuration];
        [UIView setAnimationCurve:self.animateCurve];
        [UIView setAnimationBeginsFromCurrentState:YES];
       

        CGRect emoFrame = emo.frame;
        emoFrame.origin.y = HARDWARE_SCREEN_HEIGHT - EmotionPosY;
        CGRect frame = self.frame;
        frame.origin.y = HARDWARE_SCREEN_HEIGHT -70-(_proToolHeight==0?44:_proToolHeight);
        [self setFrame:frame];
        [emo setFrame:emoFrame];
        
        [UIView commitAnimations];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:ChatToolBarMoved object:[NSString stringWithFormat:@"%f",-MoreHeight]];
    }
    else            //出现
    {
        _proToolHeight = _contentText.frame.size.height;
        //        [_sendBtn setHidden:NO];
        [_voiceChangeBtn setHidden:NO];
       
        
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:self.animateDuration];
        [UIView setAnimationCurve:self.animateCurve];
        [UIView setAnimationBeginsFromCurrentState:YES];
        
        CGRect emoFrame = emo.frame;
        emoFrame.origin.y = HARDWARE_SCREEN_HEIGHT - MoreHeight - EmotionPosY;
        //        [UIView beginAnimations:nil context:NULL];
        CGRect frame = self.frame;
        frame.origin.y = HARDWARE_SCREEN_HEIGHT- 64 - _proToolHeight - 6 - MoreHeight;
        frame.size.height = _proToolHeight + 6;
        [self setFrame:frame];
        [emo setFrame:emoFrame];
        
        [UIView commitAnimations];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:ChatToolBarMoved object:[NSString stringWithFormat:@"%f",MoreHeight]];
        
        
    }
    isMoreViewShow = !isMoreViewShow;

}

- (void)emotionBtnMethod:(id)sender
{
    if (!isVoiceShow) {
        [_voiceChangeBtn setImageStr:@"chatviewtool_voice.png" forState:UIControlStateNormal];
        
        [_voiceChangeBtn setImageStr:@"chatviewtool_voice.png" forState:UIControlStateSelected];
        
        
        [_voiceBtn setHidden:YES];
        [_contentText setHidden:NO];
        isVoiceShow = !isVoiceShow;
    }
    if (isMoreViewShow) {
        ChatToolMoreView* moreView = [ChatToolMoreView defaultMoreView];
        
        CGRect frame = moreView.frame;
        frame.origin.y = HARDWARE_SCREEN_HEIGHT-EmotionPosY;
        [moreView setFrame:frame];
   
        isMoreViewShow = !isMoreViewShow;
    }
    
    if (_contentText.internalTextView.isFirstResponder) {
        [_contentText.internalTextView resignFirstResponder];
    }

    [self emoViewShow];
}

-(void)emoViewShow
{
    EmotionView* emo = [EmotionView defaultEmoView];
    if (!isEmotionShow) {       //消失
        [_voiceChangeBtn setHidden:NO];
        
        
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:self.animateDuration];
        [UIView setAnimationCurve:self.animateCurve];
        [UIView setAnimationBeginsFromCurrentState:YES];
        
        CGRect emoFrame = emo.frame;
        emoFrame.origin.y = HARDWARE_SCREEN_HEIGHT - EmotionPosY;
        
        

        CGRect frame = self.frame;
        frame.origin.y = HARDWARE_SCREEN_HEIGHT -70-(_proToolHeight==0?44:_proToolHeight);
        [self setFrame:frame];
        [emo setFrame:emoFrame];
        
        [UIView commitAnimations];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:ChatToolBarMoved object:[NSString stringWithFormat:@"%f",-EmotionHeight]];
        [[NSNotificationCenter defaultCenter] postNotificationName:EmotionViewHidden object:nil];
    }
    else            //出现
    {
        _proToolHeight = _contentText.frame.size.height;
//        [_sendBtn setHidden:NO];
        [_voiceChangeBtn setHidden:NO];
        
        CGRect emoFrame = emo.frame;
        emoFrame.origin.y = HARDWARE_SCREEN_HEIGHT - EmotionHeight - EmotionPosY;
                
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:self.animateDuration];
        [UIView setAnimationCurve:self.animateCurve];
        [UIView setAnimationBeginsFromCurrentState:YES];
       
        
        CGRect frame = self.frame;
        frame.origin.y = HARDWARE_SCREEN_HEIGHT-64-_proToolHeight - 6 - EmotionHeight;
        frame.size.height = _proToolHeight + 6;
        [self setFrame:frame];
        [emo setFrame:emoFrame];
        
        
        [UIView commitAnimations];
        [[NSNotificationCenter defaultCenter] postNotificationName:ChatToolBarMoved object:[NSString stringWithFormat:@"%f",EmotionHeight]];
        
    }
    isEmotionShow = !isEmotionShow;
}

- (void)sendBtnMethod:(id)sender
{

    if(self.chattoolDelegate != nil && [self.chattoolDelegate respondsToSelector:@selector(sendMsg:)])
    {
        [self.chattoolDelegate sendMsg:_contentText.internalTextView.text];
    }
    else
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:SendChatMessage object:_contentText.internalTextView.text];
    }
    _contentText.text = @"";
    
    if (isMoreViewShow) {
        ChatToolMoreView* moreView = [ChatToolMoreView defaultMoreView];
            CGRect frame = moreView.frame;
            frame.origin.y = HARDWARE_SCREEN_HEIGHT-EmotionPosY;
            [moreView setFrame:frame];
        isMoreViewShow = !isMoreViewShow;
    }
    [self setNewFrameOfTool1];
    
}

-(void)setNewFrameOfTool1
{
    _proToolHeight = 44;
    
    CGRect fra =self.frame;
    fra.origin.y = HARDWARE_SCREEN_HEIGHT-70-_proToolHeight;
    fra.size.height = 6+_proToolHeight;
    [self setFrame:fra];
    [_contentText textViewDidChange:_contentText.internalTextView];
}

-(void)sendNotifices:(NSString *)text
{
    [[NSNotificationCenter defaultCenter] postNotificationName:SendChatMessage object:text];

}

- (void)voiceChangeMethod:(id)sender
{
    if (isMoreViewShow) {
        ChatToolMoreView* moreView = [ChatToolMoreView defaultMoreView];
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:self.animateDuration];
        [UIView setAnimationCurve:self.animateCurve];
        [UIView setAnimationBeginsFromCurrentState:YES];

            CGRect frame = moreView.frame;
            frame.origin.y =  HARDWARE_SCREEN_HEIGHT-EmotionPosY;
            [moreView setFrame:frame];
        isMoreViewShow = !isMoreViewShow;
        [UIView commitAnimations];
    }
    if (!isEmotionShow) {
        EmotionView* emo = [EmotionView defaultEmoView];
        
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:self.animateDuration];
        [UIView setAnimationCurve:self.animateCurve];
        [UIView setAnimationBeginsFromCurrentState:YES];

        CGRect emoFrame = emo.frame;
        emoFrame.origin.y = HARDWARE_SCREEN_HEIGHT-EmotionPosY;
        [emo setFrame:emoFrame];
        [emo setFrame:emoFrame];
        
        [UIView commitAnimations];
        isEmotionShow = !isEmotionShow;
        
    }
    if (!isVoiceShow) {
        _proToolHeight = _contentText.frame.size.height;
        [_voiceChangeBtn setImageStr:@"chatviewtool_voice.png" forState:UIControlStateNormal];
        [_voiceChangeBtn setImageStr:@"chatviewtool_voice.png" forState:UIControlStateSelected];
        [_contentText.internalTextView becomeFirstResponder];
        
        [_voiceBtn setHidden:YES];
        [_contentText setHidden:NO];
        //返回原来结果
        [self setNewFrameOfTool];
    }else{
        [_voiceChangeBtn setImageStr:@"chatviewtool_keyboard.png" forState:UIControlStateNormal];
        [_voiceChangeBtn setImageStr:@"chatviewtool_keyboard.png" forState:UIControlStateSelected];
        [_contentText.internalTextView resignFirstResponder];
//        [_voiceChangeBtn setImageStr:@"chatviewtool_keyboard.png" forState:UIControlStateHighlighted];
        
        [_voiceBtn setHidden:NO];
        [_contentText setHidden:YES];
        //返回语音
        [self setOriginFrameOfTool];
    }
    isVoiceShow = !isVoiceShow;
}

-(void)setOriginFrameOfTool
{
    _proToolHeight = 44;
    CGRect fra =self.frame;
    fra.origin.y = HARDWARE_SCREEN_HEIGHT-70-_proToolHeight;
    fra.size.height = 6+_proToolHeight;
    [self setFrame:fra];
    [[NSNotificationCenter defaultCenter] postNotificationName:ChatToolBarMoved object:[NSString stringWithFormat:@"%d",0]];
}

-(void)setNewFrameOfTool
{
    if (_proToolHeight ==0) {
        _proToolHeight = 44;
    }
    CGRect fra =self.frame;
    fra.origin.y = HARDWARE_SCREEN_HEIGHT-70-_proToolHeight;
    fra.size.height = 6+_proToolHeight;
    [self setFrame:fra];
    [_contentText textViewDidChange:_contentText.internalTextView];
    [[NSNotificationCenter defaultCenter] postNotificationName:ChatToolBarMoved object:[NSString stringWithFormat:@"%f",_proToolHeight-_contentText.frame.size.height]];
}

- (void)btnDown:(id)sender
{
    [UIUtil playSoundForVoiceAnXia];
    [NSThread sleepForTimeInterval:.25f];
    self.isSendVoice = NO;
    [self.chattoolDelegate voicebtnDown:_voiceBtn];
    
}
- (void)btnUpInside:(id)sender
{
    if (self.isSendVoice == NO) {
        [self.chattoolDelegate voicebtnUpInside:sender];
        [UIUtil playSoundForVoiceSongKai];
        self.isSendVoice = YES;
    }

}
- (void)btnDragExit:(id)sender
{
    [self.chattoolDelegate voicebtnDragExit:sender];
}
- (void)btnDragEnter:(id)sender
{
    [self.chattoolDelegate voicebtnDragEnter:sender];
}
- (void)btnUpOutside:(id)sender
{
    
    [self.chattoolDelegate voicebtnUpOutside:sender];
    
}

-(void)addEmotionStr:(NSNotification *)notification
{
    NSString *str = (NSString*)notification.object;
    if (!_contentText.internalTextView.text) {
        _contentText.internalTextView.text = @"";
    }
    
    NSRange tempRange = [_contentText.internalTextView selectedRange];
    if (self.textSelectRange.location<[_contentText.internalTextView selectedRange].location && self.textSelectRange.location!=0) {
        tempRange = self.textSelectRange;
    }
    
    NSString *firstStr = [_contentText.internalTextView.text substringToIndex:tempRange.location];
    NSString *lastStr =[_contentText.internalTextView.text substringFromIndex:tempRange.location];
    
    NSString *new_str = [NSString stringWithFormat:@"%@%@%@",firstStr,str,lastStr];
    NSRange range = NSMakeRange([[NSString stringWithFormat:@"%@%@",firstStr,str] length], 1);
    self.textSelectRange = range;
    
    [_contentText.internalTextView setText:new_str];
    [_contentText textViewDidChange:_contentText.internalTextView];
}


- (void)deleteEmotionStr{
    if ([_contentText.internalTextView.text length]>2) {
        NSMutableString *str =[NSMutableString stringWithString:_contentText.internalTextView.text];
        if ([[str substringFromIndex:[str length]-1] isEqualToString:@"]"]) {
            [str deleteCharactersInRange:NSMakeRange([str length]-1, 1)];
            for (int index=0; index<[_contentText.internalTextView.text length]-1; index++) {
                if (![[str substringFromIndex:[str length]-1] isEqualToString:@"["]) {
                    if ([[str substringFromIndex:[str length]-1] isEqualToString:@"]"]) {
                        break;
                    }
                    [str deleteCharactersInRange:NSMakeRange([str length]-1, 1)];
                }else {
                    [str deleteCharactersInRange:NSMakeRange([str length]-1, 1)];
                    [_contentText.internalTextView setText:str];
                    [_contentText textViewDidChange:_contentText.internalTextView];
                    break;
                }
                
            }
            
        }
    }
}
- (void)resetToolPositionToInitial
{
    
    _proToolHeight = 44;
    if (isMoreViewShow) {
        
        ChatToolMoreView* moreView = [ChatToolMoreView defaultMoreView];
        CGRect frame = moreView.frame;
        frame.origin.y = HARDWARE_SCREEN_HEIGHT - EmotionPosY;
        [moreView setFrame:frame];
        isMoreViewShow = NO;
        
    } else if (!isEmotionShow) {
        
        EmotionView* emo = [EmotionView defaultEmoView];
        CGRect emoFrame = emo.frame;
        emoFrame.origin.y = HARDWARE_SCREEN_HEIGHT - EmotionPosY;
        [emo setFrame:emoFrame];
        isEmotionShow = YES;
         
    } else {
        [_contentText.internalTextView resignFirstResponder];
        _contentText.placeholder = @"";
    
    }
    
    CGRect fra =self.frame;
    fra.origin.y = HARDWARE_SCREEN_HEIGHT-70-_proToolHeight;
    fra.size.height = 6+_proToolHeight;
    [self setFrame:fra];

}

- (void)resetCommentToolPositionToInitial
{
    _proToolHeight = 44;
    if (isMoreViewShow) {
        ChatToolMoreView* moreView = [ChatToolMoreView defaultMoreView];
        CGRect frame = moreView.frame;
        frame.origin.y = HARDWARE_SCREEN_HEIGHT - EmotionPosY;
        [moreView setFrame:frame];
        isMoreViewShow = NO;
        
    } else if (!isEmotionShow) {
        
        [_voiceChangeBtn setHidden:NO];
        
         EmotionView* emo = [EmotionView defaultEmoView];
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:self.animateDuration];
        [UIView setAnimationCurve:self.animateCurve];
        [UIView setAnimationBeginsFromCurrentState:YES];
        
        CGRect emoFrame = emo.frame;
        emoFrame.origin.y = HARDWARE_SCREEN_HEIGHT - EmotionPosY;
        
        
        
        CGRect frame = self.frame;
        frame.origin.y = HARDWARE_SCREEN_HEIGHT -70-(_proToolHeight==0?44:_proToolHeight);
        [self setFrame:frame];
        [emo setFrame:emoFrame];
        
        [UIView commitAnimations];
        
        isEmotionShow = YES;
        
    } else {
        [_contentText.internalTextView resignFirstResponder];
        
    }
    
}
-(void)reSetChatTool
{
    //more,emo,key,ziji

    if (_proToolHeight == 0) {
        _proToolHeight = 44;
    }
   

    
        if (isMoreViewShow) {
            ChatToolMoreView* moreView = [ChatToolMoreView defaultMoreView];
            
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:self.animateDuration];
            [UIView setAnimationCurve:self.animateCurve];
            [UIView setAnimationBeginsFromCurrentState:YES];
                CGRect frame = moreView.frame;
                frame.origin.y = HARDWARE_SCREEN_HEIGHT - EmotionPosY;
                [moreView setFrame:frame];

                isMoreViewShow = NO;
            CGRect fra =self.frame;
            fra.origin.y = HARDWARE_SCREEN_HEIGHT-70-_proToolHeight;
            fra.size.height = 6+_proToolHeight;
            [self setFrame:fra];
            
            
            [UIView commitAnimations];
            [[NSNotificationCenter defaultCenter] postNotificationName:ChatToolBarMoved object:[NSString stringWithFormat:@"%f",_proToolHeight-_contentText.frame.size.height]];
        } else if (!isEmotionShow) {
            EmotionView* emo = [EmotionView defaultEmoView];
            
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:self.animateDuration];
            [UIView setAnimationCurve:self.animateCurve];
            [UIView setAnimationBeginsFromCurrentState:YES];
            
            CGRect emoFrame = emo.frame;
            emoFrame.origin.y = HARDWARE_SCREEN_HEIGHT - EmotionPosY;
            [emo setFrame:emoFrame];
            isEmotionShow = YES;
            
            
            CGRect fra =self.frame;
            fra.origin.y = HARDWARE_SCREEN_HEIGHT-70-_proToolHeight;
            fra.size.height = 6+_proToolHeight;
            [self setFrame:fra];

            
            [UIView commitAnimations];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:ChatToolBarMoved object:[NSString stringWithFormat:@"%f",_proToolHeight-_contentText.frame.size.height]];
        } else if ([_contentText.internalTextView isFirstResponder]) {
            [_contentText.internalTextView resignFirstResponder];
            
//            [UIView beginAnimations:nil context:NULL];
//            [UIView setAnimationDuration:self.animateDuration];
//            [UIView setAnimationCurve:self.animateCurve];
//            [UIView setAnimationBeginsFromCurrentState:YES];
//            
//            CGRect fra =self.frame;
//            fra.origin.y = HARDWARE_SCREEN_HEIGHT-70-_proToolHeight;
//            fra.size.height = 6+_proToolHeight;
//            [self setFrame:fra];
//            
//            [UIView commitAnimations];

        }
    
    
    
    
    
}

-(void)dealloc
{
//    NSLog(@"ChatToolBar dealloc");
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SendTagOfEmotionIcon object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:ChatToolBarMoved object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:DeleteTagOfEmotionIcon object:nil];
}

#pragma mark-
#pragma mark --通知及其回调方法--

- (void)keyboardWillShow:(NSNotification *)notification
{
    
//    if (isMoreViewShow) {
//        return;
//    }
 
    NSDictionary *keyboardInfo = [notification userInfo];
    UIViewAnimationCurve animationCurve;
    self.animateDuration = [[keyboardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [[keyboardInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    self.animateCurve = animationCurve;
    NSValue *aValue = [keyboardInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardFrame = [aValue CGRectValue];
   
    _KeyBoardHeight = keyboardFrame.size.height;
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:self.animateDuration];
    [UIView setAnimationCurve:animationCurve];
    [UIView setAnimationBeginsFromCurrentState:YES];

    
    CGRect frame = self.frame;
   
    frame.origin.y = HARDWARE_SCREEN_HEIGHT - keyboardFrame.size.height - self.frame.size.height - 64;
    _proToolHeight = self.frame.size.height - 6;
    [self setFrame:frame];
    [UIView commitAnimations];
     [[NSNotificationCenter defaultCenter] postNotificationName:ChatToolBarMoved object:[NSString stringWithFormat:@"%f",keyboardFrame.size.height]];
}

- (void)keyboardWillhide:(NSNotification *)notification
{
//    if (isMoreViewShow) {
//        return;
//    }
    NSDictionary *keyboardInfo = [notification userInfo];
    UIViewAnimationCurve animationCurve;
    self.animateDuration = [[keyboardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [[keyboardInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    self.animateCurve = animationCurve;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:self.animateDuration];
    [UIView setAnimationCurve:animationCurve];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:self.animateDuration];
    
    _KeyBoardHeight = 0.0f;
    CGRect frame = self.frame;
    frame.origin.y = HARDWARE_SCREEN_HEIGHT  -  self.frame.size.height - 64;
    if (isMoreViewShow) {
        ChatToolMoreView* moreView = [ChatToolMoreView defaultMoreView];
        frame.origin.y = frame.origin.y - moreView.frame.size.height;
    }
    [self setFrame:frame];
    
    [UIView commitAnimations];
     [[NSNotificationCenter defaultCenter] postNotificationName:ChatToolBarMoved object:[NSString stringWithFormat:@"%f",0.0f]];
    
}
- (void)loadNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addEmotionStr:) name:SendTagOfEmotionIcon object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillhide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteEmotionStr) name:DeleteTagOfEmotionIcon object:nil];
}


- (void)setPC_APPChatFrame
{
    _voiceChangeBtn.frame = CGRectZero;
    _voiceBtn.frame = CGRectZero;
    _emotionBtn.frame = CGRectZero;
    _moreBtn.frame = CGRectMake(HARDWARE_SCREEN_WIDTH - 39.0f, 0.0f, 39.0f, 50.0f);
    CGFloat f = _contentText.frame.size.height;
    _contentText.frame = CGRectMake(10.0f, 6.0f, _moreBtn.frame.origin.x - 10.0f,f);
    _contentText.hidden = NO;
    _voiceBtn.hidden = YES;
    isVoiceShow = YES;
}

- (void)setCommentChatFrame
{
    _voiceChangeBtn.frame = CGRectZero;
    _voiceBtn.frame = CGRectZero;
    _moreBtn.frame = CGRectZero;
    _emotionBtn.frame = CGRectMake(HARDWARE_SCREEN_WIDTH - 39.0f, 0.0f, 39.0f, 50.0f);
    _contentText.frame = CGRectMake(10.0f, 6.0f, _emotionBtn.frame.origin.x - 10.0f, 44.0f);
    _contentText.hidden = NO;
    _voiceBtn.hidden = YES;
    isVoiceShow = YES;

}
- (void)setNormalChatFrame
{
    _moreBtn.frame = CGRectMake(HARDWARE_SCREEN_WIDTH - 39.0f, 0.0f, 39.0f, 50.0f);
    _emotionBtn.frame = CGRectMake(_moreBtn.frame.origin.x - 39.0f, 0, 39.0f, 50.0f);
    
    _voiceChangeBtn.frame = CGRectMake(0, 0, 44.0f, 50.0f);
    
    CGFloat f = _contentText.frame.size.height;
    _contentText.frame = CGRectMake(CGRectGetMaxX(_voiceChangeBtn.frame), 6.0f, _emotionBtn.frame.origin.x -CGRectGetMaxX(_voiceChangeBtn.frame), f);
    _voiceBtn.frame = CGRectMake(CGRectGetMaxX(_voiceChangeBtn.frame), 6.0f, _emotionBtn.frame.origin.x -CGRectGetMaxX(_voiceChangeBtn.frame), 38.0f);
}
#pragma mark - 换肤
-(void)applyTheme{
    if (_imageView) {
        UIImage* img =[[CNMultiThemeManager imageForKey:@"tabbarview_background.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 0, 0, 0) resizingMode:UIImageResizingModeStretch];
        [_imageView setImage:img];
    }
}

@end

